import { Component, OnInit } from '@angular/core';
import { TitleService } from '@core/services/title/title.service';
import { ActivatedRoute, Params } from '@angular/router';
import { ApiService } from '@core/services/http/api.service';
import { ApesMessageService } from '@shared/apes/message';
import * as CryptoJS from 'crypto-js';

@Component({
  selector: 'leaderboard',
  templateUrl: './leaderboard.component.html',
  styleUrls: ['./leaderboard.component.scss']
})
export class LeaderboardComponent implements OnInit {

  storsData: any = [];
  ioneInfo: string;

  constructor(private apiService: ApiService,
              private msg: ApesMessageService,
              private title: TitleService,
              private routeInfo: ActivatedRoute) { }

  ngOnInit() {
    this.routeInfo.queryParams.subscribe((params: Params) => {
      let key = CryptoJS.enc.Base64.parse(params['key']);
      key = JSON.parse(key.toString(CryptoJS.enc.Utf8));
      let value = CryptoJS.enc.Base64.parse(params['value']);
      value = JSON.parse(value.toString(CryptoJS.enc.Utf8));

      this.title.setTitle(value['handname']+'排行榜');
      this.getLeaderboard(key ,value['feild']);
      this.ioneInfo = value['feild'];
    });
  }

  // 获取各门店排行情况
  getLeaderboard(GSID:string, category: string) {
    let data = {'interfaceMethod': 'ipmXsrbService.getAchevementRank', 'pdata': {'GSID': GSID, 'PMJE': category }};
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            for(let list of data){
              list['MDMC'] = list['MDMC']
                .replace('(FN)', '')
                .replace('(赋能)', '')
                .replace('（FN）', '')
              ;
            }
            this.storsData = data;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }
}
